#ifndef HTTP_H
#define HTTP_H
#include <cstddef>
#include <map>
#include <string>
#include <vector>
#include <cctype>
#include <atomic>
#include <thread>
#include <mutex>
#include <set>
#include <queue>
#include <condition_variable>
#include <sys/epoll.h>
#include <netinet/in.h>
class HttpRequest {
public:
    enum class Method {
        GET,
        POST,
        PUT,
        DELETE,
        HEAD,
        OPTIONS,
        PATCH,
        UNKNOWN
    };

    enum class ParseState {
        REQUEST_LINE,
        HEADERS,
        BODY,
        COMPLETE,
        ERROR
    };

    HttpRequest() : 
        method(Method::UNKNOWN), 
        content_length(0),
        query_string(""),
        state(ParseState::REQUEST_LINE),
        body_bytes_received(0){};
    // 获取方法
    Method get_method() const { return method; }
    std::string get_method_str() const;
    
    // 获取URL
    const std::string& get_url() const { return url; }
    
    // 获取HTTP版本
    const std::string& get_http_version() const { return http_version; }
    
    // 获取头部
    const std::map<std::string, std::string>& get_headers() const { return headers; }
    
    // 获取特定头部值
    std::string get_header(const std::string& name) const;
    
    // 获取消息体
    const std::string& get_body() const { return body; }
    
    // 获取查询参数
    const std::map<std::string, std::string>& get_query_params() const { return query_params; }
    
    // 获取特定查询参数
    std::string get_query_param(const std::string& name) const;

    // 获取当前解析状态
    ParseState get_parse_state() const { return state; }

    // 重置解析状态
    void reset();
    
    // 解析数据块
    ParseState parse(const char* data, size_t length);

     const std::string& get_query() const { return query_string; }
private:
    Method method;
    std::string url;
    std::string http_version;
    std::map<std::string, std::string> headers;
    std::string body;
    std::map<std::string, std::string> query_params;
    int content_length;
    std::string query_string;
    // 解析状态机内部状态
    ParseState state;
    std::string buffer;
    int body_bytes_received;
    
    // 内部解析方法
    bool parse_request_line(const std::string& line);
    void parse_headers(const std::string& headers_data);
    void parse_query_params();
    Method parse_method(const std::string& method_str);

};
struct Connection{
    int fd;
    std::string recv_buffer;
    HttpRequest request;
    sockaddr_in client_addr;
};
HttpRequest receive_and_parse_request(int client_fd);
void start_http_server(int port = 8080,int thread_count=4);
void serve_file(int client_fd, const std::string& path, const std::string& content_type);
void http_print(const HttpRequest& request);

// ========== 包过滤相关定义 ==========
struct FirewallConfig {
    std::set<int> blocked_ports;         // 要拦截的端口
    int syn_flood_threshold = 1000;        // SYN包阈值(每秒)
    int syn_flood_ban_time = 60;         // 封禁时间(秒)
    int slow_dos_timeout = 10;           // 慢速攻击超时(秒)
    int slow_dos_min_rate = 100;         // 最小数据速率(字节/秒)
};

struct IPStats {
    int syn_count = 0;
    time_t last_syn_time = 0;
    time_t banned_until = 0;
    time_t connection_start = 0;
    size_t bytes_received = 0;
};

extern FirewallConfig firewall_config;
extern std::mutex firewall_mutex;
extern std::map<std::string, IPStats> ip_stats_map;

// 包过滤函数
bool should_block_connection(const sockaddr_in& client_addr, int port);

#endif // HTTP_H